package com.wangzh.nio.crawler;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.util.*;

public class HutoolTest {

    public static void main(String[] args) {
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlyNameNet = "http://www.haoming8.cn/baobao/7641.html";

        String familyNameNetStr = HttpUtil.get(familyNameNet);
        String boyNameNetStr = HttpUtil.get(boyNameNet);
        String girlyNameNetStr = HttpUtil.get(girlyNameNet);

//        ArrayList<String> familyList = getData(familyStr, "([^a-zA-Z]{4})(，|。)", 1);
//        ArrayList<String> boyList = getData(boyStr, "([\\u4e00-\\u9fa5·]{2})(、|。)", 1);
//        ArrayList<String> girlList = getData(girlStr, "(.. ){4}..", 0);
        //正则表达式
        List<String> familyList = ReUtil.findAll("([^a-zA-Z]{4})(，|。)", familyNameNetStr, 1);
        List<String> boyList = ReUtil.findAll("([\\u4e00-\\u9fa5·]{2})(、|。)", boyNameNetStr, 1);
        List<String> girlList = ReUtil.findAll("(.. ){4}..", girlyNameNetStr, 0);

        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyList) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                familyNameList.add(c+"");
            }
        }
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String s : boyList) {
            if (!boyNameList.contains(s)) {
                boyNameList.add(s);
            }
        }
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String names : girlList) {
            String[] name = names.split(" ");
            for (String s : name) {
                girlNameList.add(s);
            }
        }

//生产数据
        List<String> list = getInfos(familyNameList, boyNameList, girlNameList, 70, 50);
        Collections.shuffle(list);

        //拷贝到了class 目录下  D:\ideawork\java\target

        FileUtil.writeLines(list, "namesHutool.txt", CharsetUtil.UTF_8, false);

    }

    private static List<String> getInfos(ArrayList<String> familyList, ArrayList<String> boyList, ArrayList<String> girlList, int boycount, int girlcount) {

        //男姓名
        HashSet<String> boyset = new HashSet<>();
        while (true) {
            if (boyset.size() >= boycount) {
                break;
            }
            Collections.shuffle(familyList);
            Collections.shuffle(boyList);
            boyset.add(familyList.get(0) + boyList.get(0));

        }

        //女生姓名
        HashSet<String> girlset = new HashSet<>();
        while (true) {
            if (girlset.size() >= girlcount) {
                break;
            }
            Collections.shuffle(familyList);
            Collections.shuffle(girlList);
            girlset.add(familyList.get(0) + girlList.get(0));

        }

        //组合姓名  张三-男-23
        List<String> hs = new ArrayList<>();
        Random r = new Random();
        for (String s : boyset) {
            int age = r.nextInt(10) + 18;
            hs.add(s + "-男-" + age);
        }

        for (String s : girlset) {
            int age = r.nextInt(10) + 15;
            hs.add(s + "-女-" + age);
        }


        return hs;
    }

}
